package com.shengxian.basic.mylogin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shengxian.basic.mylogin.config.JwtUtils;
import com.shengxian.basic.mylogin.config.MD5;
import com.shengxian.basic.mylogin.entity.Sample;
import com.shengxian.basic.mylogin.entity.UserDenglu;
import com.shengxian.basic.mylogin.entity.baseinfo;
import com.shengxian.basic.mylogin.entity.qiye_renzheng;
import com.shengxian.basic.mylogin.mapper.UserDengLuMapper;
import com.shengxian.basic.mylogin.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Api(description = "登录管理")
@RestController
@RequestMapping("/login")
@CrossOrigin
@Slf4j
public class logincontroller {

    @Autowired
    private UserDengLuMapper myUserDengLuMapper;

//----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = "查找所有用户 ")
    @GetMapping("findAll")
    public String findAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<UserDenglu> userDenglus = myUserDengLuMapper.selectList(null);
        System.out.println(userDenglus);
        return userDenglus.toString();
    }

// ----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = "用户注册")
    @GetMapping("zhuce_password{name}/{password}")
    public String loginpassword(@PathVariable String name, @PathVariable String password) {
        UserDenglu userDenglu = new UserDenglu();
        userDenglu.setName(name);
        userDenglu.setPassword(MD5.string2MD5(password));
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("name", name);
        List<UserDenglu> UserDengluList = myUserDengLuMapper.selectByMap(mymap);
        if (UserDengluList.isEmpty()) {
            System.out.println(" 没有    可以添加");
            myUserDengLuMapper.insert(userDenglu);
            return "添加成功";
        } else {
            System.out.println(" 已经有  不能添加");
            return "添加失败，用户已存在";
        }

    }

//----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = "用户登录，密码")
    @GetMapping("login_password{name}/{password}")
    public String denglu_password(@PathVariable String name, @PathVariable String password) {
        Map<String, Object> mymap = new HashMap<>();
//    考虑用户不存在的情况
//    先查询是否存在   多条情况下的异常
//     token串传给前台
//  通过登录 获取用户信息，生成token串传给前台  MD5加密
//        每个登录要绑定基本信息
        mymap.put("name", name);
        mymap.put("password", MD5.string2MD5(password));

        List<UserDenglu> usersdenglu = myUserDengLuMapper.selectByMap(mymap);
        System.out.println("--------------------wp---------------------");
        System.out.println(usersdenglu);
        if (usersdenglu.isEmpty()) {
            return " i am null,不能登录";
        } else {
            return " i am true , 可以登录 ";
        }

    }
//----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = "手机注册")
    @GetMapping("zhuce_phone{name}/{phone}")
    public String phone_zhuce(@PathVariable String name, @PathVariable String phone) throws Exception {

// ==========================================================================================
        UserDenglu userDenglu = new UserDenglu();
        userDenglu.setName(name);
        userDenglu.setPhone(phone);
        Map<String, Object> mymap = new HashMap<>();
        mymap.put("name", name);
        List<UserDenglu> UserDengluList = myUserDengLuMapper.selectByMap(mymap);
        if (UserDengluList.isEmpty()) {
            System.out.println(" 没有    可以添加");
            myUserDengLuMapper.insert(userDenglu);
            return "添加成功";
        } else {
            System.out.println(" 已经有  不能添加");
            return "添加失败，用户已存在";
        }

    }


//----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = "手机验证码登录")
    @GetMapping("login_phonecode{ {phone}")
    public String messagetest(@PathVariable String phone) throws Exception {
//        Scanner input = new Scanner(System.in);
//
//        String phone =  "16601273758";
        String[] strs = new String[]{"12", "34"};
        String phonecode = Sample.maintest(phone, strs);
        return "验证码是 ： " + phonecode;

    }

//----------------------------------------------------------------------------------------------------------------------------

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {


        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UserDenglu member = myUserDengLuMapper.selectById(memberId);
        System.out.println("==================Token===============");
        System.out.println(member);
        return R.ok().data("userInfo", member);
    }
//----------------------------------------------------------------------------------------------------------------------------

    @ApiOperation(value = " 修改用户登录表")
    @PostMapping("updateUserDenglu")
    public void updateuser(@RequestBody UserDenglu myUserDenglu) {

        UpdateWrapper<UserDenglu> updateWrapper = new UpdateWrapper();
        updateWrapper.set("name", myUserDenglu.getName())
                .set("phone", myUserDenglu.getPhone())
                .set("password", myUserDenglu.getPassword())
                .set("token", myUserDenglu.getToken())
                .set("wxnickname", myUserDenglu.getWxnickname())
                .set("wxheadimgurl", myUserDenglu.getWxheadimgurl())
                .set("wxopenid", myUserDenglu.getWxopenid())
                .eq("id", myUserDenglu.getId());


//        以下更新失败
//        {       "id": 1,
//                "name": "wp",
//                "password": "123456",
//                "phone": "16601273758",
//                "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzaGVuZ3hpYW4iLCJpYXQiOjE2NTUxNzM2MDgsImV4cCI6MTY1NTI2MDAwOCwiaWQiOiIxIiwibmFtZSI6IuW-ruWwmCJ9.YRmvSNaKp6rqNHvvBpWNM8loBYAHr55lbKehtLvzLmQ",
//                "uniqueuserid": "xxx",
//                "wxheadimgurl":"https://thirdwx.qlogo.cn/mmopen/vi_32/iaBSypaHhqmnic3dggr0gcEnNrEric6cqUzdyDBcribKWeRG8yenzV32r1SN7CLRAm8UYoFrwGiaib3xCuAla9kTWZfQ/132",
//                "wxnickname": "微尘",
//                "wxopenid":"o3_SC5y_zlOrd9GGU6N31NQ5DPfw"
//        }




        myUserDengLuMapper.update(null, updateWrapper);

    }
//----------------------------------------------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------------------------------------------


//----------------------------------------------------------------------------------------------------------------------------


}
